package com.huizluo.module2.game;

import java.util.Scanner;

public class Game {
    private static final int WHITE = 1;//1 表示白方
    private static final int BLACK = 2;//表示黑方
    private int whoPlay = 2;//2 表示黑走，1表示白走;默认黑方先走
    private int[][] chessboard =  new int[][]{
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    };
    //绘制棋盘
    private void draw(int[][] chessboard){
        for (int i = 0;i<16;i++){
            for (int j = 0;j<16;j++){
                if (j == 15){
                    System.out.printf("%d \n",chessboard[i][j]);
                }else{
                    System.out.printf("%d ",chessboard[i][j]);
                }
            }
        }
    }
    //下棋
    public void Play(){
        boolean stop = false;
        //绘制棋盘
        this.draw(this.chessboard);
        //开始下棋
        while (!stop){
            if (this.whoPlay == Game.WHITE){
                System.out.println("白方下");
            }else{
                System.out.println("黑方下");
            }
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next();
            int[] in = this.checkInput(input);
            if (in == null){
                System.out.println("输入有误，请重新下！！！");
                continue;
            }
            //修改棋盘
            if (this.whoPlay == Game.WHITE){
                this.chessboard[in[0]][in[1]] = Game.WHITE;
            }else{
                this.chessboard[in[0]][in[1]] = Game.BLACK;
            }
            //重新绘制
            this.draw(this.chessboard);

            Status s = this.validate(this.whoPlay);

            if (s == Status.NO_WON){
                if (this.whoPlay == Game.WHITE){
                    this.whoPlay = Game.BLACK;
                }else{
                    this.whoPlay = Game.WHITE;
                }
            }else if(s == Status.BLACK_OWN){
                System.out.println(Status.BLACK_OWN.getDesc());
                stop = true;
            }else{
                System.out.println(Status.WHITE_OWN.getDesc());
                stop = true;
            }
        }

        System.out.println("GAME OVER!!!");
    }
    //检查输入
    private int[] checkInput(String input){
        String[] data = input.split(",");
        //输入不是一个坐标
        if (data.length != 2){
            return null;
        }
       int x = Integer.parseInt(data[0]);
       int y = Integer.parseInt(data[1]);
       //超出棋盘范围
       if (x< 0 || x >15 || y<0 || y>15){
           return null;
       }
       //已经被下了
       if (this.chessboard[x][y] != 0){
           return null;
       }
       int[] arr = new int[]{x,y};
       return arr;
    }

    //检查输赢
    private Status validate(int whoPlay){
       int sum = 0;

        //从左上到右下检查，所以遇到到旗子只需要在判断 左下，下，右，右下，四个方向
        START:
        for (int i=0;i<16;i++){
            for (int j=0;j<16;j++){
                if (this.chessboard[i][j] == whoPlay){
                    boolean find = true;
                    //sum = 1;
                    int ii = i;
                    int jj = j;

                    //向右边查找
                    while (find && jj < 15){
                        if (this.chessboard[ii][jj++] == whoPlay){
                            sum++;
                            if (sum == 5){
                                break START;
                            }
                        }else{
                            find = false;
                        }
                    }

                    find = true;
                    sum = 0;
                    ii = i;
                    jj = j;
                    //向下边查找
                    while (find && ii < 15){
                        if (this.chessboard[ii++][jj] == whoPlay){
                            sum++;
                            if (sum == 5){
                                break START;
                            }
                        }else{
                            find = false;
                        }
                    }

                    find = true;
                    sum = 0;
                    ii = i;
                    jj = j;
                    //向左下边查找
                    while (find && ii < 15 && jj > 0){
                        if (this.chessboard[ii++][jj--] == whoPlay){
                            sum++;
                            if (sum == 5){
                                break START;
                            }
                        }else{
                            find = false;
                        }
                    }

                    find = true;
                    sum = 0;
                    ii = i;
                    jj = j;
                    //向右下方查找
                    while (find && ii <15 && jj <15){
                        if (this.chessboard[ii++][jj++] == whoPlay){
                            sum++;
                            if (sum == 5){
                                break START;
                            }
                        }else{
                            find = false;
                        }
                    }

                    sum = 0;

                }
            }
        }

        if (sum == 5){
            if (whoPlay == Game.WHITE){
                return Status.WHITE_OWN;
            }else{
                return Status.BLACK_OWN;
            }
        }else{
            return Status.NO_WON;
        }
    }
}
